/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package TreeView;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import fr.ove.openmath.jome.model.*;
import fr.ove.openmath.jome.model.events.*;

public class View extends Panel implements ModelListener {
    TreeTick tick;
    private Panel node;
    private BorderLabel properties;
    private Panel children;
    
    public View() {
        setLayout(new BorderLayout());
        
        tick = new TreeTick();
        add("West", tick);
        
        node = new Panel();
        node.setLayout(new BorderLayout());
        add("Center", node);
        
        Panel panelProperties = new Panel();
        panelProperties.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 1));
        node.add("North", panelProperties);
        properties = new BorderLabel();
        panelProperties.add(properties);
        
        children = new Panel();
        children.setLayout(new OneColumnLayout());
        node.add("Center", children);
        
        properties.setBackground(Color.lightGray);
    }
    
    public Component add(Component c) {
        children.add(c);
        return c;
    }
    
    public void removeAll() {
        children.removeAll();
    }
    
    public void setProperties(String prop) {
        properties.setText("   " + prop);
    }
    
    public Dimension getPreferredSize() {
        Dimension dim = super.getPreferredSize();
        float gap = ((float)(properties.getPreferredSize()).height) / 2.0f;
        tick.setVGap(gap / ((float) dim.height));
        
        return dim;
    }
    

    /**
    * Consumes (i.e. treats) the event received from the model.
    * @param modelEvent the event to consume.
    */
    public void consumeModelEvent(ModelEvent modelEvent) {
        int action = modelEvent.getAction();
        switch (action) {
            case ModelEvent.CREATE :
                FormulaTreeStructure fts = (FormulaTreeStructure) modelEvent.getSource();
                String prop = "FORMULA  --[ ";
                prop += "Strahler = " + fts.getNbStrahler();
                prop += ", Depth = " + fts.getDepth();
                prop += ", Width = " + fts.getNbChildren();
                prop += " ]";
                
                setProperties(prop);
                
                buildDisplay(fts);
                
                getParent().validate();
                //getParent().repaint();
                break;

            case ModelEvent.CLEAR :
                children.removeAll();
        }
    }

    
    public void buildDisplay(FormulaTreeStructure fts) {
        FormulaTreeStructure ftsChild;
        int count = fts.getNbChildren();
        View childDisplay;

        // On parcourt la liste des fils de la fts, pour leur associer le display qui va bien
        for (int i = 0; i < count; i++) {
            childDisplay = new View();
            
            ftsChild = (FormulaTreeStructure) fts.getChild(i);
            String id = ftsChild.getResourceIdentifier();
            String prop = ftsChild.getValue() + "  --[ ";
            prop += "Strahler = " + ftsChild.getNbStrahler();
            prop += ", Depth = " + ftsChild.getDepth();
            prop += ", Width = " + ftsChild.getNbChildren();
            prop += " ]";
            
            childDisplay.setProperties(prop);
            
            if (i == (count -1))
                childDisplay.tick.setTickType(TreeTick.BOTTOM);
            else
                childDisplay.tick.setTickType(TreeTick.MIDDLE);
            
            // On ajoute le nouveau display comme fils à l'instance
            children.add(childDisplay);
            // On descend dans la fts pour poursuivre la construction des display
            childDisplay.buildDisplay(ftsChild);
        }

    }

}
